package com.gdziarmaga.webshop.client.swing;

import java.awt.Button;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gdziarmaga.webshop.utils.springcontext.AppContext;
import com.gdziarmaga.webshopwebservices.BookByISBNResponse.BookDetail;
import com.gdziarmaga.webshopwebservices.WebShopWebServices;

@Service
public class ClientPanel extends JFrame {

	@Autowired
	private WebShopWebServices webShopWebServices;

	private TextField title;
	private TextField issue_year;
	private TextField long_description;
	private TextField pages;
	private TextField publisher;
	private TextField section;
	private TextField price;

	public void init() {
		webShopWebServices = (WebShopWebServices) AppContext
				.getApplicationContext().getBean("webShopServiceClient");
	}

	public ClientPanel() {
		this.setLayout(new GridLayout(15, 1));

		final TextField field = new TextField();
		field.setSize(300, 50);
		Button button = new Button("Pobierz");
		JPanel firstElement = new JPanel(new GridLayout(1, 2));
		firstElement.add(field);
		firstElement.add(button);

		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String isbn = field.getText();
				// 83-7361-684-5
				BookDetail bookDetail = webShopWebServices.bookByISBN(isbn);
				fillFields(bookDetail);
			}
		});

		this.getContentPane().add(firstElement);

		title = new TextField();
		this.getContentPane().add(new JLabel("Tytuł:"));
		this.getContentPane().add(title);

		issue_year = new TextField();
		this.getContentPane().add(new JLabel("Rok wydania:"));
		this.getContentPane().add(issue_year);

		pages = new TextField();
		this.getContentPane().add(new JLabel("Liczba stron:"));
		this.getContentPane().add(pages);

		long_description = new TextField();
		this.getContentPane().add(new JLabel("Opis:"));
		this.getContentPane().add(long_description);

		publisher = new TextField();
		this.getContentPane().add(new JLabel("Wydawnictwo:"));
		this.getContentPane().add(publisher);

		section = new TextField();
		this.getContentPane().add(new JLabel("Dział:"));
		this.getContentPane().add(section);

		price = new TextField();
		this.getContentPane().add(new JLabel("Cena:"));
		this.getContentPane().add(price);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setSize(880, 560);
		this.setVisible(true);

	}

	protected void fillFields(BookDetail bookDetail) {
		title.setText(bookDetail.getTitle());
		issue_year.setText(bookDetail.getIssueYear() + "");
		long_description.setText(bookDetail.getLongDescription());
		pages.setText(bookDetail.getPages() + "");
		publisher.setText(bookDetail.getPublisher());
		section.setText(bookDetail.getSection());
		price.setText(bookDetail.getPrice() + "");

		this.getContentPane().repaint();
	}

	public WebShopWebServices getWebShopWebServices() {
		return webShopWebServices;
	}

	public void setWebShopWebServices(WebShopWebServices webShopWebServices) {
		this.webShopWebServices = webShopWebServices;
	}
}
